<template>
  <view class="container">
    <!-- 小程序头部兼容 -->
    <!-- #ifdef MP -->
    <view class="mp-search-box">
      <input class="ser-input" type="text" value="输入关键字搜索" disabled />
    </view>
    <!-- #endif -->

    <!-- 头部轮播 -->
    <view class="carousel-section">
      <!-- 标题栏和状态栏占位符 -->
      <!-- <view class="titleNview-placing"></view> -->
      <!-- 背景色区域 -->
      <view
        class="titleNview-background"
        :style="{ backgroundColor: titleNViewBackground }"
      ></view>
      <swiper class="carousel" circular @change="swiperChange" :autoplay="true">
        <swiper-item
          v-for="(item, index) in advertiseList"
          :key="index"
          class="carousel-item"
          @click="navToAdvertisePage(item)"
        >
          <image :src="item.pic" />
        </swiper-item>
      </swiper>
      <!-- 自定义swiper指示器 -->
      <view class="swiper-dots">
        <text class="num">{{ swiperCurrent + 1 }}</text>
        <text class="sign">/</text>
        <text class="num">{{ swiperLength }}</text>
      </view>
    </view>
    <!-- 头部功能区 -->
    <!-- <view class="cate-section">
			<view class="cate-item">
				<image src="/static/temp/c3.png"></image>
				<text>专题</text>
			</view>
			<view class="cate-item">
				<image src="/static/temp/c5.png"></image>
				<text>话题</text>
			</view>
			<view class="cate-item">
				<image src="/static/temp/c6.png"></image>
				<text>优选</text>
			</view>
			<view class="cate-item">
				<image src="/static/temp/c7.png"></image>
				<text>特惠</text>
			</view>
		</view> -->

    <!-- 品牌制造商直供 -->
    <view class="f-header m-t" @click="navToRecommendBrandPage()">
      <image src="/static/icon_home_brand.png"></image>
      <view class="tit-box">
        <text class="tit">品牌制造商直供</text>
        <text class="tit2">工厂直达消费者，剔除品牌溢价</text>
      </view>
      <text class="yticon icon-you"></text>
    </view>

    <view class="guess-section">
      <view
        v-for="(item, index) in brandList"
        :key="index"
        class="guess-item"
        @click="navToBrandDetailPage(item)"
      >
        <view class="image-wrapper-brand">
          <image :src="item.logo" mode="aspectFit"></image>
        </view>
        <text class="title clamp brand">{{ item.name }}</text>
        <!-- <text class="title2">商品数量：{{ item.productCount }}</text> -->
      </view>
    </view>

    <!-- 秒杀专区 -->
    <view class="f-header m-t" v-if="homeFlashPromotion !== null">
      <image src="/static/icon_flash_promotion.png"></image>
      <view class="tit-box">
        <text class="tit">秒杀专区</text>
        <text class="tit2"
          >下一场 {{ homeFlashPromotion.nextStartTime | formatTime }} 开始</text
        >
      </view>
      <view class="tit-box countdown">
        <text class="tit2" style="text-align: right">本场结束剩余：</text>
        <view style="text-align: right">
          <uni-countdown
            :font-size="13"
            :hour="cutDownTime.endHour"
            :minute="cutDownTime.endMinute"
            :second="cutDownTime.endSecond"
            color="#FFFFFF"
            background-color="#cb573c"
          />
          <!-- <text class="hour timer">{{ cutDownTime.endHour }}</text>
          <text>:</text>
          <text class="minute timer">{{ cutDownTime.endMinute }}</text>
          <text>:</text>
          <text class="second timer">{{ cutDownTime.endSecond }}</text> -->
        </view>
      </view>
      <text class="yticon icon-you" v-show="false"></text>
    </view>

    <view class="guess-section">
      <view
        v-for="(item, index) in homeFlashPromotion.productList"
        :key="index"
        class="guess-item"
        @click="navToDetailPage(item)"
      >
        <view class="image-wrapper">
          <image :src="item.pic" mode="aspectFill"></image>
        </view>
        <text class="title clamp">{{ item.name }}</text>
        <text class="title2 clamp">{{ item.subTitle }}</text>
        <text class="price">￥{{ item.price }}</text>
      </view>
    </view>

    <!-- 新鲜好物 -->
    <view class="f-header m-t" @click="navToNewProudctListPage()">
      <image src="/static/icon_new_product.png"></image>
      <view class="tit-box">
        <text class="tit">新鲜好物</text>
        <text class="tit2">为你寻觅世间好物</text>
      </view>
      <text class="yticon icon-you"></text>
    </view>
    <view class="seckill-section">
      <scroll-view class="floor-list" scroll-x>
        <view class="scoll-wrapper">
          <view
            v-for="(item, index) in newProductList"
            :key="index"
            class="floor-item"
            @click="navToDetailPage(item)"
          >
            <image :src="item.pic" mode="aspectFill"></image>
            <text class="title clamp">{{ item.name }}</text>
            <text class="title2 clamp">{{ item.subTitle }}</text>
            <text class="price">￥{{ item.price }}</text>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 人气推荐楼层 -->
    <view class="f-header m-t" @click="navToHotProudctListPage()">
      <image src="/static/icon_hot_product.png"></image>
      <view class="tit-box">
        <text class="tit">人气推荐</text>
        <text class="tit2">大家都赞不绝口的</text>
      </view>
      <text class="yticon icon-you"></text>
    </view>

    <view class="hot-section">
      <view
        v-for="(item, index) in hotProductList"
        :key="index"
        class="guess-item"
        @click="navToDetailPage(item)"
      >
        <view class="image-wrapper">
          <image :src="item.pic" mode="aspectFill"></image>
        </view>
        <view class="txt">
          <text class="title clamp">{{ item.name }}</text>
          <text class="title2">{{ item.subTitle }}</text>
          <text class="price">￥{{ item.price }}</text>
        </view>
      </view>
    </view>

    <!-- 猜你喜欢-->
    <view class="f-header m-t">
      <image src="/static/icon_recommend_product.png"></image>
      <view class="tit-box">
        <text class="tit">猜你喜欢</text>
        <text class="tit2">你喜欢的都在这里了</text>
      </view>
      <text class="yticon icon-you" v-show="false"></text>
    </view>

    <view class="guess-section">
      <view
        v-for="(item, index) in recommendProductList"
        :key="index"
        class="guess-item"
        @click="navToDetailPage(item)"
      >
        <view class="image-wrapper">
          <image :src="item.pic" mode="aspectFill"></image>
        </view>
        <text class="title clamp">{{ item.name }}</text>
        <text class="title2 clamp">{{ item.subTitle }}</text>
        <text class="price">￥{{ item.price }}</text>
      </view>
    </view>
    <uni-load-more :status="loadingType"></uni-load-more>
  </view>
</template>

<script>
import { fetchContent, fetchRecommendProductList } from '@/api/home.js'
import { formatDate } from '@/utils/date'
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
export default {
  components: {
    uniLoadMore,
  },
  data() {
    return {
      titleNViewBackground: '',
      titleNViewBackgroundList: ['rgb(203, 87, 60)'],
      swiperCurrent: 0,
      swiperLength: 0,
      carouselList: [],
      goodsList: [],
      advertiseList: [],
      brandList: [],
      homeFlashPromotion: [],
      newProductList: [],
      hotProductList: [],
      recommendProductList: [],
      recommendParams: {
        pageNum: 1,
        pageSize: 4,
      },
      loadingType: 'more',
    }
  },
  onLoad() {
    this.loadData()
  },
  //下拉刷新
  onPullDownRefresh() {
    this.recommendParams.pageNum = 1
    this.loadData()
  },
  //加载更多
  onReachBottom() {
    this.recommendParams.pageNum++
    this.loadingType = 'loading'
    fetchRecommendProductList(this.recommendParams).then((response) => {
      let addProductList = response.data
      if (response.data.length === 0) {
        //没有更多了
        this.recommendParams.pageNum--
        this.loadingType = 'nomore'
      } else {
        this.recommendProductList =
          this.recommendProductList.concat(addProductList)
        this.loadingType = 'more'
      }
    })
  },
  computed: {
    cutDownTime() {
      let endTime = new Date(this.homeFlashPromotion.endTime)
      let endDateTime = new Date()
      let startDateTime = new Date()
      endDateTime.setHours(endTime.getHours())
      endDateTime.setMinutes(endTime.getMinutes())
      endDateTime.setSeconds(endTime.getSeconds())
      let offsetTime = endDateTime.getTime() - startDateTime.getTime()
      let endHour = Math.floor(offsetTime / (60 * 60 * 1000))
      let offsetMinute = offsetTime % (60 * 60 * 1000)
      let endMinute = Math.floor(offsetMinute / (60 * 1000))
      let offsetSecond = offsetTime % (60 * 1000)
      let endSecond = Math.floor(offsetSecond / 1000)
      return {
        endHour: endHour,
        endMinute: endMinute,
        endSecond: endSecond,
      }
    },
  },
  filters: {
    formatTime(time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      let date = new Date(time)
      return formatDate(date, 'hh:mm:ss')
    },
  },
  methods: {
    /**
     * 加载数据
     */
    async loadData() {
      fetchContent().then((response) => {
        console.log('onLoad', response.data)
        this.advertiseList = response.data.advertiseList
        this.swiperLength = this.advertiseList.length
        this.titleNViewBackground = this.titleNViewBackgroundList[0]
        this.brandList = response.data.brandList
        this.homeFlashPromotion = response.data.homeFlashPromotion
        this.newProductList = response.data.newProductList
        this.hotProductList = response.data.hotProductList
        fetchRecommendProductList(this.recommendParams).then((response) => {
          this.recommendProductList = response.data
          uni.stopPullDownRefresh()
        })
      })
    },
    //轮播图切换修改背景色
    swiperChange(e) {
      const index = e.detail.current
      this.swiperCurrent = index
      let changeIndex = index % this.titleNViewBackgroundList.length
      this.titleNViewBackground = this.titleNViewBackgroundList[changeIndex]
    },
    //商品详情页
    navToDetailPage(item) {
      let id = item.id
      uni.navigateTo({
        url: `/pages/product/product?id=${id}`,
      })
    },
    //广告详情页
    navToAdvertisePage(item) {
      let id = item.id
      console.log('navToAdvertisePage', item)
    },
    //品牌详情页
    navToBrandDetailPage(item) {
      let id = item.id
      uni.navigateTo({
        url: `/pages/brand/brandDetail?id=${id}`,
      })
    },
    //推荐品牌列表页
    navToRecommendBrandPage() {
      uni.navigateTo({
        url: `/pages/brand/list`,
      })
    },
    //新鲜好物列表页
    navToNewProudctListPage() {
      uni.navigateTo({
        url: `/pages/product/newProductList`,
      })
    },
    //人气推荐列表页
    navToHotProudctListPage() {
      uni.navigateTo({
        url: `/pages/product/hotProductList`,
      })
    },
  },
  // #ifndef MP
  // 标题栏input搜索框点击
  onNavigationBarSearchInputClicked: async function (e) {
    this.$api.msg('点击了搜索框')
  },
  //点击导航栏 buttons 时触发
  onNavigationBarButtonTap(e) {
    const index = e.index
    if (index === 0) {
      // this.$api.msg('点击了皇家官网')
      window.location.href = 'https://www.royal-power.com/index.php'
    }
  },
  // #endif
}
</script>

<style lang="scss">
/* #ifdef MP */
.mp-search-box {
  position: absolute;
  left: 0;
  top: 30upx;
  z-index: 9999;
  width: 100%;
  padding: 0 80upx;

  .ser-input {
    flex: 1;
    height: 56upx;
    line-height: 56upx;
    text-align: center;
    font-size: 28upx;
    color: $font-color-base;
    border-radius: 20px;
    background: rgba(255, 255, 255, 0.6);
  }
}

page {
  .cate-section {
    position: relative;
    z-index: 5;
    border-radius: 16upx 16upx 0 0;
    margin-top: -20upx;
  }

  .carousel-section {
    padding: 0;

    .titleNview-placing {
      padding-top: 0;
      height: 0;
    }

    .carousel {
      .carousel-item {
        padding: 0;
      }
    }

    .swiper-dots {
      left: 45upx;
      bottom: 40upx;
    }
  }
}

/* #endif */

page {
  background: #f5f5f5;
}

.m-t {
  margin-top: 16upx;
}

/* 头部 轮播图 */
.carousel-section {
  position: relative;
  padding-top: 10px;

  .titleNview-placing {
    height: var(--status-bar-height);
    padding-top: 44px;
    box-sizing: content-box;
  }

  .titleNview-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 340upx;
    transition: 0.4s;
  }
}

.carousel {
  width: 100%;
  height: 350upx;

  .carousel-item {
    width: 100%;
    height: 100%;
    padding: 0 28upx;
    overflow: hidden;
  }

  image {
    width: 100%;
    height: 100%;
    border-radius: 10upx;
  }
}

.swiper-dots {
  display: flex;
  position: absolute;
  left: 60upx;
  bottom: 15upx;
  width: 72upx;
  height: 36upx;
  background-image: url();
  background-size: 100% 100%;

  .num {
    width: 36upx;
    height: 36upx;
    border-radius: 50px;
    font-size: 24upx;
    color: #fff;
    text-align: center;
    line-height: 36upx;
  }

  .sign {
    position: absolute;
    top: 0;
    left: 50%;
    line-height: 36upx;
    font-size: 12upx;
    color: #fff;
    transform: translateX(-50%);
  }
}

/* 分类 */
.cate-section {
  display: flex;
  justify-content: space-around;
  align-items: center;
  flex-wrap: wrap;
  padding: 30upx 22upx;
  background: #fff;

  .cate-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: $font-sm + 2upx;
    color: $font-color-dark;
  }

  /* 原图标颜色太深,不想改图了,所以加了透明度 */
  image {
    width: 88upx;
    height: 88upx;
    margin-bottom: 14upx;
    border-radius: 50%;
    opacity: 0.7;
    box-shadow: 4upx 4upx 20upx rgba(250, 67, 106, 0.3);
  }
}

.ad-1 {
  width: 100%;
  height: 210upx;
  padding: 10upx 0;
  background: #fff;

  image {
    width: 100%;
    height: 100%;
  }
}

/* 秒杀专区 */
.seckill-section {
  padding: 4upx 30upx 24upx;
  background: #fff;

  .s-header {
    display: flex;
    align-items: center;
    height: 92upx;
    line-height: 1;

    .s-img {
      width: 140upx;
      height: 30upx;
    }

    .tip {
      font-size: $font-base;
      color: $font-color-light;
      margin: 0 20upx 0 40upx;
    }

    .timer {
      display: inline-block;
      width: 40upx;
      height: 36upx;
      text-align: center;
      line-height: 36upx;
      margin-right: 14upx;
      font-size: $font-sm + 2upx;
      color: #fff;
      border-radius: 2px;
      background: rgba(0, 0, 0, 0.8);
    }

    .icon-you {
      font-size: $font-lg;
      color: $font-color-light;
      flex: 1;
      text-align: right;
    }
  }

  .floor-list {
    white-space: nowrap;
  }

  .scoll-wrapper {
    display: flex;
    align-items: flex-start;
  }

  .floor-item {
    width: 300upx;
    margin-right: 20upx;
    font-size: $font-sm + 2upx;
    color: $font-color-dark;
    line-height: 1.8;

    image {
      width: 300upx;
      height: 300upx;
      border-radius: 6upx;
    }

    .price {
      color: $uni-color-primary;
    }
  }

  .title2 {
    font-size: $font-sm;
    color: $font-color-light;
    line-height: 40upx;
  }
}

.f-header {
  display: flex;
  align-items: center;
  height: 140upx;
  padding: 6upx 30upx 8upx;
  background: #fff;
  border-bottom: 1upx solid #f5f5f5;

  image {
    flex-shrink: 0;
    width: 80upx;
    height: 80upx;
    margin-right: 20upx;
  }

  .tit-box {
    flex: 1;
    display: flex;
    flex-direction: column;
  }
  .countdown {
    align-items: flex-end;
  }

  .tit {
    font-size: $font-lg + 2upx;
    color: #font-color-dark;
    line-height: 1.3;
  }

  .tit2 {
    font-size: $font-sm;
    color: $font-color-light;
  }

  .icon-you {
    font-size: $font-lg + 2upx;
    color: $font-color-light;
  }

  .timer {
    display: inline-block;
    width: 40upx;
    height: 36upx;
    text-align: center;
    line-height: 36upx;
    margin-right: 14upx;
    font-size: $font-sm + 2upx;
    color: #fff;
    border-radius: 2px;
    background: rgba(0, 0, 0, 0.8);
  }
}

/* 分类推荐楼层 */
.hot-floor {
  width: 100%;
  overflow: hidden;
  margin-bottom: 20upx;

  .floor-img-box {
    width: 100%;
    height: 320upx;
    position: relative;

    &:after {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background: linear-gradient(rgba(255, 255, 255, 0.06) 30%, #f8f8f8);
    }
  }

  .floor-img {
    width: 100%;
    height: 100%;
  }

  .floor-list {
    white-space: nowrap;
    padding: 20upx;
    padding-right: 50upx;
    border-radius: 6upx;
    margin-top: -140upx;
    margin-left: 30upx;
    background: #fff;
    box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
    position: relative;
    z-index: 1;
  }

  .scoll-wrapper {
    display: flex;
    align-items: flex-start;
  }

  .floor-item {
    width: 180upx;
    margin-right: 20upx;
    font-size: $font-sm + 2upx;
    color: $font-color-dark;
    line-height: 1.8;

    image {
      width: 180upx;
      height: 180upx;
      border-radius: 6upx;
    }

    .price {
      color: $uni-color-primary;
    }
  }

  .more {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    flex-shrink: 0;
    width: 180upx;
    height: 180upx;
    border-radius: 6upx;
    background: #f3f3f3;
    font-size: $font-base;
    color: $font-color-light;

    text:first-child {
      margin-bottom: 4upx;
    }
  }
}

/* 猜你喜欢 */
.guess-section {
  display: flex;
  flex-wrap: wrap;
  padding: 30upx 30upx 0 30upx;
  background: #fff;

  .guess-item {
    display: flex;
    flex-direction: column;
    width: 48%;
    padding-bottom: 40upx;

    &:nth-child(2n + 1) {
      margin-right: 4%;
    }
  }

  .image-wrapper {
    width: 100%;
    height: 330upx;
    border-radius: 3px;
    overflow: hidden;

    image {
      width: 100%;
      height: 100%;
      opacity: 1;
    }
  }

  .image-wrapper-brand {
    width: 100%;
    height: 150upx;
    border-radius: 3px;
    overflow: hidden;

    image {
      width: 100%;
      height: 100%;
      opacity: 1;
    }
  }

  .title {
    font-size: $font-lg;
    color: $font-color-dark;
    line-height: 80upx;
  }

  .title2 {
    font-size: $font-sm;
    color: $font-color-light;
    line-height: 40upx;
  }

  .price {
    font-size: $font-lg;
    color: $uni-color-primary;
    line-height: 1;
  }
  .brand {
    text-align: center;
  }
}

.hot-section {
  display: flex;
  flex-wrap: wrap;
  padding: 0 30upx;
  background: #fff;

  .guess-item {
    display: flex;
    flex-direction: row;
    width: 100%;
    padding-bottom: 40upx;
  }

  .image-wrapper {
    width: 30%;
    height: 250upx;
    border-radius: 3px;
    overflow: hidden;

    image {
      width: 100%;
      height: 100%;
      opacity: 1;
    }
  }

  .title {
    font-size: $font-lg;
    color: $font-color-dark;
    line-height: 80upx;
  }

  .title2 {
    font-size: $font-sm;
    color: $font-color-light;
    line-height: 40upx;
    height: 80upx;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
  }

  .price {
    font-size: $font-lg;
    color: $uni-color-primary;
    line-height: 80upx;
  }

  .txt {
    width: 70%;
    display: flex;
    flex-direction: column;
    padding-left: 40upx;
  }
}
</style>
